﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace Framework.Platform.Components.UI.Authentication
{
    public partial class LoginForm : Form, ILoginUI
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.OkPressed();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.CancelPressed();
        }

        #region ILoginUI Members

        public event EventHandler<LoginUISubmitEventArgs> Submit;

        public event EventHandler<EventArgs> Cancel;

        public string UserName
        {
            get
            {
                return this.tb_UserName.Text;
            }
            set
            {
                if (this.tb_UserName.Text != value)
                    this.tb_UserName.Text = value;
            }
        }

        public string Password
        {
            get
            {
                return this.tb_Password.Text;
            }
            set
            {
                if (this.tb_Password.Text != value)
                    this.tb_Password.Text = value;
            }
        }

        public void HideUI()
        {
            this.Hide();
        }

        public void ShowUI()
        {
            this.ShowDialog();
        }

        public void LoginFailed(string errorMessage)
        {
            this.ShowBadUserPassword();
        }
        #endregion

        private void OkPressed()
        {
            this.HideAllToolTips();

            if (this.tb_UserName.Text.Length == 0)
            {
                this.ShowUserNameRequired();
                return;

            }

            if (String.IsNullOrEmpty(this.tb_Password.Text))
            {
                this.ShowPasswordRequired();
                return;
            }

            this.tb_UserName.Enabled = false;
            this.tb_Password.Enabled = false;
            this.btn_OK.Enabled = false;
            this.btn_Cancel.Enabled = false;

            Application.DoEvents();
            this.OnOkClicked(this, new LoginUISubmitEventArgs(this.UserName, this.Password));
        }

        private void CancelPressed()
        {
            this.OnCancelClicked(this, new EventArgs());
            this.Close();
        }

        private void HideAllToolTips()
        {
            this.toolTipBadUserPassword.Hide(this.tb_Password);
            this.toolTipPasswordRequired.Hide(this.tb_Password);
            this.toolTipUserRequired.Hide(this.tb_UserName);
        }

        public void ShowBadUserPassword()
        {
            if (!this.Disposing)
            {
                this.Reset();

                this.toolTipPasswordRequired.Hide(this.tb_Password);

                this.tb_Password.Text = string.Empty;

                this.tb_Password.Focus();

                this.toolTipBadUserPassword.ToolTipTitle = "Incorrect user or password";
                this.toolTipBadUserPassword.SetToolTip(this.tb_Password, "Did you forget your user name or password?\r\nThe entered user name or password could not be validated.");
                this.toolTipBadUserPassword.Show("Did you forget your user name or password?\r\nThe entered user name or password could not be validated.", this.tb_Password, 5000);

                Application.DoEvents();
                SystemSounds.Asterisk.Play();
            }
        }

        private void ShowUserNameRequired()
        {
            if (!this.Disposing)
            {
                this.toolTipPasswordRequired.Hide(this.tb_Password);

                this.tb_UserName.Focus();

                this.toolTipUserRequired.ToolTipTitle = "User name is required";
                this.toolTipUserRequired.SetToolTip(this.tb_UserName, "The user name must be entered to continue.");
                this.toolTipUserRequired.Show("The user name must be entered to continue.", this.tb_UserName, 5000);
            }
        }

        private void ShowPasswordRequired()
        {
            if (!this.Disposing)
            {
                this.toolTipUserRequired.Hide(this.tb_UserName);

                this.tb_Password.Focus();

                this.toolTipPasswordRequired.ToolTipTitle = "Password is required";
                this.toolTipPasswordRequired.SetToolTip(this.tb_Password, "The password must be entered to continue.");
                this.toolTipPasswordRequired.Show("The password must be entered to continue.", this.tb_Password, 5000);
            }
        }

        private void OnOkClicked(object sender, LoginUISubmitEventArgs e)
        {
            if (this.Submit != null)
            {
                this.Submit(sender, e);
            }
        }

        private void OnCancelClicked(object sender, EventArgs e)
        {
            if (this.Cancel != null)
            {
                this.Cancel(sender, e);
            }
        }

        public void Reset()
        {
            if (!this.Disposing)
            {
                Application.DoEvents();

                this.tb_UserName.Enabled = true;
                this.tb_Password.Enabled = true;
                this.btn_OK.Enabled = true;
                this.btn_Cancel.Enabled = true;
            }
        }

        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.CancelPressed();
        }
    }
}
